package zbum.issue.issue.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springmodules.validation.commons.DefaultBeanValidator;

import zbum.issue.code.service.CodeService;
import zbum.issue.issue.service.IssueDefaultVO;
import zbum.issue.issue.service.IssueService;
import zbum.issue.issue.service.IssueVO;
import zbum.issue.issue.service.SearchVO;
import egovframework.rte.fdl.property.EgovPropertyService;

/**
 * @Class Name : IssueController.java
 * @Description : Issue Controller class
 * @Modification Information
 *
 * @author zbum
 * @since 2010-07-21
 * @version 1.0
 * @see
 *  
 *  Copyright (C)  All right reserved.
 */

@Controller
@SessionAttributes({"issueVO","",""})
public class IssueController {

    @Resource(name = "issueService")
    private IssueService issueService;
    
    @Resource(name = "codeService")
    private CodeService codeService;
    
    /** EgovPropertyService */
    @Resource(name = "propertiesService")
    private EgovPropertyService propertiesService;
    
    @Resource(name="beanValidator")
    private DefaultBeanValidator beanValidator;
    
	
    /**
	 * ISSUE 목록을 조회한다. (pageing)
	 * @param searchVO - 조회할 정보가 담긴 IssueDefaultVO
	 * @return "/issue/IssueList"
	 * @exception Exception
	 */
    @RequestMapping(value="/issue/IssueList.do")
    public String selectIssueList(@ModelAttribute("searchVO") IssueDefaultVO searchVO,
    		ModelMap model) throws Exception {
     	return "/issue/IssueList";
    } 
    
    /**
	 * ISSUE 목록을 조회한다. (pageing)
	 * @param searchVO - 조회할 정보가 담긴 IssueDefaultVO
	 * @return "/issue/IssueList"
	 * @exception Exception
	 */
    @RequestMapping(value="/issue/IssueList.ajax")
    public ModelAndView selectIssueList( @ModelAttribute("searchVO") SearchVO searchVO )
            throws Exception {
    	
    	ModelAndView mav = new ModelAndView("jqgridXmlView");
		
        List issueList = issueService.selectIssueList(searchVO);

        int totCnt = issueService.selectIssueListTotCnt(searchVO);
		
		mav.addObject("page", searchVO.getPage());
		mav.addObject("total", totCnt/Integer.parseInt(searchVO.getRows())+1);
		mav.addObject("records", totCnt);
		mav.addObject("rows", issueList);
		
        mav.addObject("jqgrid_data", issueList);
        mav.addObject("jqgrid_columns", new String[]{"id","subject","regDate","occurDate", "dueDate","currentStatusCodeNm"});
        
        return mav;	
    } 
    
    @RequestMapping(value="/issue/addIssue.do", method=RequestMethod.GET)
    public String addIssueView(
            @ModelAttribute("searchVO") IssueDefaultVO searchVO, Model model)
            throws Exception {
    	IssueVO issueVO = new IssueVO();
    	SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
    	issueVO.setOccurDate(format.format(new Date()));
        model.addAttribute("issueVO", issueVO);
        
        
        model.addAttribute("codeList",codeService.selectCodeListByCodeGroupId("001"));
        return "/issue/IssueRegister";
    }
    
    @RequestMapping(value="/issue/addIssue.do", method=RequestMethod.POST)
    public String addIssue(	HttpSession session, IssueVO issueVO, @ModelAttribute("searchVO") IssueDefaultVO searchVO, BindingResult bindingResult, SessionStatus status)
            throws Exception {
    	if ( session.getAttribute("issueVO") == null ) {
    		return "forward:/issue/IssueList.do";
    	}
    	
    	beanValidator.validate(issueVO, bindingResult);
    	System.out.println("======>"+bindingResult.hasErrors());
    	if ( bindingResult.hasErrors() ) {
    		return "/issue/IssueRegister";
    	}
    	
        issueService.insertIssue(issueVO);
    	
        status.setComplete();
        return "forward:/issue/IssueList.do";
    }
    
    @RequestMapping(value="/issue/updateIssue.do", method=RequestMethod.GET)
    public String updateIssueView(
            @RequestParam("id") java.lang.String id ,
            @ModelAttribute("searchVO") IssueDefaultVO searchVO, Model model)
            throws Exception {
        IssueVO issueVO = new IssueVO();
        issueVO.setId(id);
 
        // 변수명은 CoC 에 따라 issueVO
        model.addAttribute(selectIssue(issueVO, searchVO));
        model.addAttribute("codeList",codeService.selectCodeListByCodeGroupId("001"));
        return "/issue/IssueRegister";
    }

    @RequestMapping("/issue/selectIssue.do")
    public @ModelAttribute("issueVO")
    IssueVO selectIssue(
            IssueVO issueVO,
            @ModelAttribute("searchVO") IssueDefaultVO searchVO) throws Exception {
        return issueService.selectIssue(issueVO);
    }

    @RequestMapping(value="/issue/updateIssue.do", method=RequestMethod.POST)
    public String updateIssue(
            IssueVO issueVO,
            @ModelAttribute("searchVO") IssueDefaultVO searchVO, SessionStatus status)
            throws Exception {
        issueService.updateIssue(issueVO);
        status.setComplete();
        return "forward:/issue/IssueList.do";
    }
    
    @RequestMapping("/issue/deleteIssue.do")
    public String deleteIssue(
            IssueVO issueVO,
            @ModelAttribute("searchVO") IssueDefaultVO searchVO, SessionStatus status)
            throws Exception {
        issueService.deleteIssue(issueVO);
        status.setComplete();
        return "forward:/issue/IssueList.do";
    }

}
